<?php
/**
 * 首页接口
 */
require(dirname(dirname(__FILE__))."/includes/initInterface.php");
require(dirname(dirname(__FILE__))."/includes/cls_pictureModel.php");
require(dirname(dirname(__FILE__))."/includes/cls_loginModel.php");
require(dirname(dirname(__FILE__))."/includes/jssdk.php");

$action  = isset($_REQUEST['act']) ? trim($_REQUEST['act']) : '';
$picture  = new Picture();
$picture->$action();
Class Picture {
	private $m;//自己的model
	private $jsonp;
	public function __construct(){

		$this->jsonp = new JSONP();
		//基础信息构建
		$this->m = new pictureModel();
		//微信分享参数构建
	}
	
	
	public function getJssdk()
	{
	    global $_CODE;
	    
	    
	    
	    $m = new loginModel();
	    $res = $m->get_appid_ser();
	    //微信分享参数构建
	    $appid=$res['appid'];
	    $secret=$res['appsecret'];
	    
	    $jsdk = new JSSDK($appid,$secret);
	    $jssdk = $jsdk->getSignPackage();
	    
	    die($this->jsonp->JSONP($_CODE['MAIN_GETMAININFO_SUCCESS'],$jssdk));
	   // var_dump($jssdk);
	    
	}
	
	public function uploadData()
	{
	    global $_CODE;
	    
	    $medias = $_REQUEST['media'];
	    
	    $desc = $_REQUEST['desc'];
	    
	    $create_id = isset($_REQUEST['wxid'])?intval($_REQUEST['wxid']):$_SESSION['wxid'];
	    if(empty($medias))
	    {
	        $code = $_CODE['PUBLISH_EMPTY_PICTURE'];
	        
	    }
	    elseif(empty($desc))
	    {
	        $code = $_CODE['PUBLISH_EMPTY_DESC'];
	    }
	    elseif($create_id<=0)
	    {
	    	$code = $_CODE['PUBLISH_EMPTY_WXID'];
	    }
	    else
	    {
	        
	        $info['pic'] = $this->getPic($medias);
	        
	        $info['create_time'] = yf_time();
	        
// 	        $desc = json_encode($desc); //暴露出unicode
	        //$desc = preg_replace("#(\\\ue[0-9a-f]{3})#ie","addslashes('\\1')",$desc); //将emoji的
	        //$desc = json_decode($desc); //暴露出unicode
	        $info['desc'] = $desc;
	        
	        //模拟已登陆
	        
// 	        $_SESSION['wxid'] = 123;
	        
	        $info['create_id'] = $create_id;
	        
	        $back = $this->m->insert_pic($info);
	        
	        $code = $_CODE['PUBLISH_SUCCESS'];
	        
            
	    }
	    
	    die($this->jsonp->JSONP($code));
	}
	
	
	
	public function testThumb()
	{
	    
	    $temp = "../../Uploads/img/201511/1371448529389kk.jpg";
	    
	    
    	    
    	    //$temp = str_replace(ROOT_PATH, "../../", $img);
    	    
//     	    $open=fopen("log.txt","a" ); 
    	    
//     	    fwrite($open,$url); 
    	    
//     	    fclose($open);
    	    
    	    $dst_temp = str_replace("img", "thumb", $temp);
    	    
    	    
    	    
    	    $file = ROOT_PATH.'Uploads/thumb/'.date('Ym');
    	    
     	    if(!is_dir($file))
     	    {
    	       
    	        
    	       mkdir($file);
     	    }
    	    
    	    
    	    mkThumbnail($temp,640,null,$dst_temp);
    	    
    	    $pic = str_replace(ROOT_PATH, "/", $img);
    	    
    	    $thumb = str_replace("img", "thumb", $pic);
    	    
    	    $pictures['img'][] = $pic;
    	    
    	    $pictures['thumb'][] = $thumb;
    	    
    	    var_dump($thumb);
    	    
	}
	
	public function getPic($medias)
	{
	    
	    $m = new loginModel();
	    $res = $m->get_appid_ser();
	    //微信分享参数构建
	    $appid=$res['appid'];
	    $secret=$res['appsecret'];
	    
	    $jsdk = new JSSDK($appid,$secret);
	    
	    $token = $jsdk->getAccessToken();
	    
// 	    echo $token;
	    
	    $pictures = array();
	    
	    
	    
	    foreach ($medias as $k => $v)
	    {
	        $media_id = $v;
	        
	        
	        
    	    $url="http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=".$token."&media_id=".$media_id;
    	    
    	    $img = $k.rand(0, 100).time()."kk.jpg";
    	    
    	    $img = $this->GrabImage($url, $img);
    	    
    	    $temp = str_replace(ROOT_PATH, "../../", $img);
    	    
    	    $open=fopen("log.txt","a" ); 
    	    
    	    fwrite($open,$url); 
    	    
    	    fclose($open);
    	    
    	    $dst_temp = str_replace("img", "thumb", $temp);
    	    
    	    
    	    
    	    $file = ROOT_PATH.'Uploads/thumb/'.date('Ym');
    	    
     	    if(!is_dir($file))
     	    {
    	       
    	        
    	       mkdir($file);
     	    }
    	    
    	    
    	    mkThumbnail($temp,200,null,$dst_temp);
    	    
    	    $pic = str_replace(ROOT_PATH, "/", $img);
    	    
    	    $thumb = str_replace("img", "thumb", $pic);
    	    
    	    $pictures['img'][] = $pic;
    	    
    	    $pictures['thumb'][] = $thumb;
    	    
	    }
	    
	    
	    return $pictures;

	   	    
	}
	
	
	public function GrabImage($url, $filename = "") {
	    if($url == ""):return false;endif;
	    if($filename == "") {
	        $ext = strrchr($url, ".");
	        if($ext != ".gif" && $ext != ".jpg"):return false;endif;
	        $filename = date("YmdHis").$ext;
	    }
	    ob_start();
	    readfile($url);
	    $img = ob_get_contents();
	    ob_end_clean();
	    $size = strlen($img);
	    $file = ROOT_PATH.'Uploads/img/'.date('Ym');
	    //$file = 'Uploads/img/'.date('Ym');
	    if(!is_dir($file))
	    {
	        mkdir($file);
	    }
	    $file = $file."/".$filename;
	    $fp2 = @fopen($file, "a");
	    fwrite($fp2, $img);
	    fclose($fp2);
	    
	    
	    
	    return $file;
	}
	
	public function getSessionWxid()
	{
		global $_CODE;
		$wxid = isset($_SESSION['wxid'])?intval($_SESSION['wxid']):0;
		if($wxid>0)
		{
			die($this->jsonp->JSONP($_CODE['PICTURE_GETSESSIONWXID_YES'],$wxid));
		}
		die($this->jsonp->JSONP($_CODE['PICTURE_GETSESSIONWXID_NO']));
	}
}